# Contributor: Erik Larsson <who+alpine@cnackers.org>
# Contributor: Alexander Sack <asac@pantacor.com>
# Maintainer: Alexander Sack <asac@pantacor.com>
pkgname=tpm2-tss
pkgver=4.0.1
pkgrel=2
pkgdesc="TPM 2.0 TSS"
url="https://github.com/tpm2-software/tpm2-tss/"
pkgusers="tss"
pkggroups="tss"
arch="all"
license="BSD-2-Clause"
makedepends="
	cmocka-dev
	curl-dev
	json-c-dev
	linux-headers
	openssl-dev>3
	perl
	util-linux-dev
	"
_depends_fapi="acl"
subpackages="
	$pkgname-doc
	$pkgname-mu
	$pkgname-sys
	$pkgname-esys
	$pkgname-tcti-device
	$pkgname-tcti-mssim
	$pkgname-tcti-swtpm
	$pkgname-tcti-cmd
	$pkgname-tcti-pcap
	$pkgname-tcti-spi-helper:spi_helper
	$pkgname-policy
	$pkgname-rc
	$pkgname-tctildr
	$pkgname-fapi
	$pkgname-static
	$pkgname-dev
	"
install="tpm2-tss-fapi.pre-install tpm2-tss-fapi.pre-upgrade tpm2-tss-fapi.post-install tpm2-tss-fapi.post-upgrade"
source="
	https://github.com/tpm2-software/tpm2-tss/releases/download/$pkgver/tpm2-tss-$pkgver.tar.gz
	"
# fhs: /var/run/tpm2-tss/eventlog created in package()
options="!fhs"

case "$CARCH" in
s390x)
	# https://github.com/tpm2-software/tpm2-tss/issues/2531
	options="$options !check"
	;;
esac

prepare() {
 	default_prepare
	update_config_sub
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--with-tctidefaultmodule=device \
		--with-tctidefaultconfig=/dev/tpmrm0 \
		--with-crypto=ossl \
		--enable-unit
	make
}

check() {
	make check || {
		cat ./test-suite.log
		exit 1
	}
}

package() {
	make DESTDIR="$pkgdir" install
	rm -rf "$pkgdir"/etc/tmpfiles.d
	rm -rf "$pkgdir"/etc/sysusers.d

	mkdir -p "$pkgdir"/var/run/tpm2-tss/eventlog/
}

mu() {
	pkgdesc="TPM 2.0 Marshaling libraries"

	amove usr/lib/libtss2-mu.so \
		usr/lib/libtss2-mu.so.*
}

sys() {
	pkgdesc="TPM 2.0 System API libraries"

	amove usr/lib/libtss2-sys.so \
		usr/lib/libtss2-sys.so.*
}

esys() {
	pkgdesc="TPM 2.0 Enhanced System API libraries"

	amove usr/lib/libtss2-esys.so \
		usr/lib/libtss2-esys.so.*
}

device() {
	pkgdesc="TPM 2.0 Device TCTI"
	provides="libtss2-tcti"
	provider_priority="100"

	amove usr/lib/libtss2-tcti-device.so \
		usr/lib/libtss2-tcti-device.so.*
}

mssim() {
	pkgdesc="TPM 2.0 Simulator TCTI"
	provides="libtss2-tcti"
	provider_priority="10"

	amove usr/lib/libtss2-tcti-mssim.so \
		usr/lib/libtss2-tcti-mssim.so.*
}

rc() {
	pkgdesc="TPM 2.0 RC libraries"

	amove usr/lib/libtss2-rc.so \
		usr/lib/libtss2-rc.so.*
}

tctildr() {
	pkgdesc="TPM 2.0 TCTI loader libraries"

	amove usr/lib/libtss2-tctildr.so \
		usr/lib/libtss2-tctildr.so.*
}

fapi() {
	depends="$_depends_fapi"
	pkgdesc="TPM 2.0 FAPI libraries"

	amove usr/lib/libtss2-fapi.so \
		usr/lib/libtss2-fapi.so.* \
		etc/tpm2-tss/fapi-config.json \
		etc/tpm2-tss/fapi-profiles

	mkdir -p "$subpkgdir"/var/lib/tpm2-tss/system/keystore/
}

swtpm() {
	pkgdesc="TPM 2.0 SWTPM TCTI"
	provides="libtss2-tcti"
	provider_priority="10"

	amove usr/lib/libtss2-tcti-swtpm.so \
		usr/lib/libtss2-tcti-swtpm.so.*
}

cmd() {
	pkgdesc="TPM 2.0 CMD TCTI"
	provides="libtss2-tcti"
	provider_priority="10"

	amove usr/lib/libtss2-tcti-cmd.so \
		usr/lib/libtss2-tcti-cmd.so.*
}

pcap() {
	pkgdesc="TPM 2.0 PCAP TCTI"
	provides="libtss2-tcti"
	provider_priority="10"

	amove usr/lib/libtss2-tcti-pcap.so \
		usr/lib/libtss2-tcti-pcap.so.*
}

spi_helper() {
	pkgdesc="TPM 2.0 spi helper TCTI"
	provides="libtss2-tcti"
	provider_priority="10"

	amove usr/lib/libtss2-tcti-spi-helper.so \
		usr/lib/libtss2-tcti-spi-helper.so.*
}

policy() {
	pkgdesc="TPM 2.0 policy library"

	amove usr/lib/libtss2-policy.so \
		usr/lib/libtss2-policy.so.*
}

sha512sums="
ed6ddc52cb0e8c1082a4bb001e1225eb9905fd2380da88db5fd69ff5b5d9d43a93eb67b634e49d53eb5d586832da3aef2c4c7e5f18d51bb730481f8913319d7d  tpm2-tss-4.0.1.tar.gz
"
